package com.sunda.spmsweb.overseacontroller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sunda.spmscommon.ResponseResult;
import com.sunda.spmsoversea.dto.OverseaOtherDTO;
import com.sunda.spmsoversea.dto.OverseaOtherSearchDTO;
import com.sunda.spmsoversea.dto.OverseaOtherUpdateDTO;
import com.sunda.spmsoversea.entity.OverseaOtherDtl;
import com.sunda.spmsoversea.service.IOverseaOtherTaskService;
import com.sunda.spmsweb.util.JWTUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 海外仓其他出入库 前端控制器
 * </p>
 *
 * @author Wayne
 * @since 2021-12-27
 */
@RestController
@RequestMapping("/overseaOtherTask")
@Api(tags = "海外仓其他出入库", description = "海外仓其他出入库")
public class OverseaOtherTaskController {

    @Autowired
    IOverseaOtherTaskService iOverseaOtherTaskService;

    @RequestMapping("/getOtherTaskList")
    @ApiOperation(value = "查询其他出入库表头分页", notes = "查询其他出入库表头分页\n" +
            "movementType:移动类型(报废出库551;零价格出库Z55;零价格入库Z56;收货多收Z16;串货调整309;不可抗力出库Z13;IT专用出库Z47;期初库存增加561;期初库存减少562;) \n" +
            "spmsStatus:SPMS状态(0已删除;1保存;2提交/待审批;3审批不通过;4审批通过;5提交SAP失败;6提交SAP成功;)\n" +
            "createDate:单据创建起止日期格式 yyyy-MM-dd\n" +
            "{\n" +
            "\t\"createDateBegin\": \"\",\n" +
            "\t\"createDateEnd\": \"\",\n" +
            "\t\"current\": 1,\n" +
            "\t\"movementType\": \"\",\n" +
            "\t\"size\": 20,\n" +
            "\t\"spmsId\": \"\",\n" +
            "\t\"spmsStatus\": \"\",\n" +
            "\t\"werks\": \"\",\n" +
            "\t\"whsLocationCode\": \"\"\n" +
            "}\n" +
            "\n", httpMethod = "POST")
    public ResponseResult getOtherTaskList(@RequestBody OverseaOtherSearchDTO overseaOtherSearchDTO){
        try {
            String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            IPage iPage = iOverseaOtherTaskService.getOtherTaskPage(overseaOtherSearchDTO);
            return ResponseResult.success().add("otherTaskList", iPage);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/getOtherDtlList")
    @ApiOperation(value = "查询其他出入库明细数据", notes = "查询其他出入库单明细数据\n" +
            "示例参数uuid：d41c2af86717669fe0530101007f008c", httpMethod = "POST")
    public ResponseResult getOtherDtlList(@RequestParam String uuid){
        try {
            return ResponseResult.success().add("otherDtlList", iOverseaOtherTaskService.getOtherDtlList(uuid));
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/closeOtherTask")
    @ApiOperation(value = "其他出入库任务关闭", notes = "其他出入库任务关闭\n" +
            "单据状态说明：0已删除;1保存;2提交/待审批;3审批不通过;4审批通过;5提交SAP失败;6提交SAP成功;" +
            "此接口单据状态变化：1-0；3-0；" +
            "示例参数uuid：d41c2af86717669fe0530101007f008c, dataVersion = 5310", httpMethod = "POST")
    public ResponseResult closeOtherTask(@RequestParam String uuid, @RequestParam Integer dataVersion){
        try {
            String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            return iOverseaOtherTaskService.closeOtherTask(uuid, dataVersion, userId);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }


    @RequestMapping("/createOtherTask")
    @ApiOperation(value = "创建其他出入库任务", notes = "创建其他出入库任务\n" +
            "spmsStatus：SPMS状态(0已删除;1保存;2提交/待审批;3审批不通过;4审批通过;5提交SAP失败;6提交SAP成功;)\n" +
            "movementType：报废出库551;零价格出库Z55;零价格入库Z56;收货多收Z16;串货调整309;不可抗力出库Z13;IT专用出库Z47;期初库存增加561;期初库存减少562;建安固定资产领用Z57;建安固定资产领用撤销Z58;\n" +
            "单据创建初始状态为 1 或 2；\n" +
            "示例参数：\n" +
            "{\n" +
            "\t\"uuid\": \"\",\n" +
            "\t\"sapDeliveryNote\": null,\n" +
            "\t\"movementType\": \"Z56\",\n" +
            "\t\"werks\": \"GF31\",\n" +
            "\t\"workshopCode\": null,\n" +
            "\t\"whsLocationCode\": \"1006\",\n" +
            "\t\"createDate\": \"2021-12-28\",\n" +
            "\t\"operateTitle\": \"零价格入库Z56——20211228\",\n" +
            "\t\"comments\": \"comments\",\n" +
            "\t\"costCenter\": null,\n" +
            "\t\"spmsStatus\": \"1\",\n" +
            "\t\"remarks\": \"remarks\",\n" +
            "\t\"otherDtlList\": [{\n" +
            "\t\t\"materialNo\": \"140000270\",\n" +
            "\t\t\"storageInfoAdjusted\": null,\n" +
            "\t\t\"sapDeliveryNoteItem\": null,\n" +
            "\t\t\"supplierNo\": null,\n" +
            "\t\t\"actualQtyBasicUnit\": 3,\n" +
            "\t\t\"basicUnit\": \"PCS\",\n" +
            "\t\t\"storageInfo\": null,\n" +
            "\t\t\"sapDeliveryNote\": null,\n" +
            "\t\t\"actualQtyBasicUnitAdjusted\": 0,\n" +
            "\t\t\"remarks\": \"ee\",\n" +
            "\t\t\"comments\": \"\"\n" +
            "\t}, {\n" +
            "\t\t\"materialNo\": \"210031306\",\n" +
            "\t\t\"storageInfoAdjusted\": null,\n" +
            "\t\t\"sapDeliveryNoteItem\": null,\n" +
            "\t\t\"supplierNo\": null,\n" +
            "\t\t\"actualQtyBasicUnit\": 5,\n" +
            "\t\t\"basicUnit\": \"PCS\",\n" +
            "\t\t\"storageInfo\": null,\n" +
            "\t\t\"sapDeliveryNote\": null,\n" +
            "\t\t\"actualQtyBasicUnitAdjusted\": 0,\n" +
            "\t\t\"remarks\": \"rr\",\n" +
            "\t\t\"comments\": \"\",\n" +
            "\t\t\"internalOrderNumber\": \"abc123\",\n" +
            "\t\t\"localCurrency\": \"1000\"\n" +
            "\t}]\n" +
            "}", httpMethod = "POST")
    public ResponseResult createOtherTask(@RequestBody OverseaOtherDTO overseaOtherDTO){
        try {
            String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            return iOverseaOtherTaskService.createOtherTask(overseaOtherDTO, userId);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/updateOtherTask")
    @ApiOperation(value = "更新其他出入库任务", notes = "更新其他出入库任务\n" +
            "spmsStatus：SPMS状态(0已删除;1保存;2提交/待审批;3审批不通过;4审批通过;5提交SAP失败;6提交SAP成功;)\n" +
            "movementType：报废出库551;零价格出库Z55;零价格入库Z56;收货多收Z16;串货调整309;不可抗力出库Z13;IT专用出库Z47;期初库存增加561;期初库存减少562;建安固定资产领用Z57;建安固定资产领用撤销Z58;\n" +
            "更新任务允许状态变化：\n" +
            "   1-1；保存修改；\n" +
            "   1-2；提交审批：更新单据状态备注等信息，更新明细数量储位备注等信息；按行校验库存扣件是否够；\n" +
            "   2-4；审批通过：更新单据状态，按行校验库存扣件是否够，按行扣减/增加仓库库存；\n" +
            "   3-4；审批通过\n" +
            "示例参数：\n" +
            "{\n" +
            "\t\"uuid\": \"d41c2af86717669fe0530101007f008c\",\n" +
            "\t\"operateTitle\": \"零价格入库Z56——20211227\",\n" +
            "\t\"costCenter\": \"\",\n" +
            "\t\"spmsStatus\": \"1\",\n" +
            "\t\"remarks\": \"remark\",\n" +
            "\t\"comments\": \"remark\",\n" +
            "\t\"dataVersion\": 5461,\n" +
            "\t\"otherDtlList\": [{\n" +
            "\t\t\"actualQtyBasicUnit\": 3,\n" +
            "\t\t\"actualQtyBasicUnitAdjusted\": 0,\n" +
            "\t\t\"comments\": \"cc\",\n" +
            "\t\t\"item\": \"10\",\n" +
            "\t\t\"materialNoAdjusted\": \"\",\n" +
            "\t\t\"remarks\": \"rr\",\n" +
            "\t\t\"sapDeliveryNote\": \"\",\n" +
            "\t\t\"sapDeliveryNoteItem\": \"\",\n" +
            "\t\t\"storageInfo\": \"[{\\\"storageUuid\\\":\\\"cbd9576a4e53461fe0530101007f3e24\\\",\\\"qty\\\":3,\\\"storageNo\\\":\\\"A010101\\\",\\\"unitValue\\\":3}]\",\n" +
            "\t\t\"storageInfoAdjusted\": \"\",\n" +
            "\t\t\"supplierNo\": \"\",\n" +
            "\t\t\"uuid\": \"d41c2af86717669fe0530101007f008c\"\n" +
            "\t}, {\n" +
            "\t\t\"actualQtyBasicUnit\": 5,\n" +
            "\t\t\"actualQtyBasicUnitAdjusted\": 0,\n" +
            "\t\t\"comments\": \"cc\",\n" +
            "\t\t\"item\": \"20\",\n" +
            "\t\t\"materialNoAdjusted\": \"\",\n" +
            "\t\t\"remarks\": \"rr\",\n" +
            "\t\t\"internalOrderNumber\": \"abc123\",\n" +
            "\t\t\"localCurrency\": \"1000\",\n" +
            "\t\t\"sapDeliveryNote\": \"\",\n" +
            "\t\t\"sapDeliveryNoteItem\": \"\",\n" +
            "\t\t\"storageInfo\": \"[{\\\"storageUuid\\\":\\\"cbd9576a4e53461fe0530101007f3e24\\\",\\\"qty\\\":5,\\\"storageNo\\\":\\\"A010101\\\",\\\"unitValue\\\":5}]\",\n" +
            "\t\t\"storageInfoAdjusted\": \"\",\n" +
            "\t\t\"supplierNo\": \"\",\n" +
            "\t\t\"uuid\": \"d41c2af86717669fe0530101007f008c\"\n" +
            "\t}]\n" +
            "}", httpMethod = "POST")
    public ResponseResult updateOtherTask(@RequestBody OverseaOtherUpdateDTO overseaOtherUpdateDTO){
        try {
            String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            return iOverseaOtherTaskService.updateOtherTask(overseaOtherUpdateDTO, userId);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/updateOtherDtlListI")
    @ApiOperation(value = "新增其他出入库明细行", notes = "新增其他出入库明细行\n" +
            "spmsStatus：SPMS状态(0已删除;1保存;2提交/待审批;3审批不通过;4审批通过;5提交SAP失败;6提交SAP成功;)\n" +
            "此功能仅对状态 1、3 可用；\n" +
            "示例参数：\n" +
            "[{\n" +
            "\t\"comments\": \"ccc\",\n" +
            "\t\"materialNo\": \"140000270\",\n" +
            "\t\"costCenter\": null,\n" +
            "\t\"storageInfoAdjusted\": null,\n" +
            "\t\"sapDeliveryNoteItem\": null,\n" +
            "\t\"supplierNo\": null,\n" +
            "\t\"actualQtyBasicUnit\": 3,\n" +
            "\t\"basicUnit\": \"PCS\",\n" +
            "\t\"materialEnDescAdjusted\": null,\n" +
            "\t\"uuid\": \"b7e6a112fe8f47508d1ec7a8ce56bb88\",\n" +
            "\t\"storageInfo\": \"[{\\\"storageUuid\\\":\\\"cbd9576a4e53461fe0530101007f3e24\\\",\\\"qty\\\":3,\\\"storageNo\\\":\\\"A010101\\\",\\\"unitValue\\\":3}]\",\n" +
            "\t\"sapDeliveryNote\": null,\n" +
            "\t\"actualQtyBasicUnitAdjusted\": 0,\n" +
            "\t\"remarks\": \"ee\",\n" +
            "\t\"internalOrderNumber\": \"abc123\",\n" +
            "\t\"localCurrency\": \"1000\"\n" +
            "}]", httpMethod = "POST")
    public ResponseResult updateOtherDtlListI(@RequestBody List<OverseaOtherDtl> otherDtlList){
        try {
            String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            return iOverseaOtherTaskService.updateOtherDtlListI(otherDtlList, userId);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/updateOtherDtlListD")
    @ApiOperation(value = "删除其他出入库明细行", notes = "删除其他出入库明细行\n" +
            "spmsStatus：SPMS状态(0已删除;1保存;2提交/待审批;3审批不通过;4审批通过;5提交SAP失败;6提交SAP成功;)\n" +
            "此功能仅对状态 1、3 可用；\n" +
            "示例参数：\n" +
            "\n" +
            "[{\n" +
            "\t\"item\": \"40\",\n" +
            "\t\"uuid\": \"b7e6a112fe8f47508d1ec7a8ce56bb88\"\n" +
            "}]", httpMethod = "POST")
    public ResponseResult updateOtherDtlListD(@RequestBody List<OverseaOtherDtl> otherDtlList){
        try {
            String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            return iOverseaOtherTaskService.updateOtherDtlListD(otherDtlList, userId);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/submitOtherTaskSap")
    @ApiOperation(value = "其他出入库提交SAP", notes = "其他出入库提交SAP\n" +
            "spmsStatus：SPMS状态(0已删除;1保存;2提交/待审批;3审批不通过;4审批通过;5提交SAP失败;6提交SAP成功;)\n" +
            "提交SAP；4-5/6、5-5/6，此功能仅对状态 4、5 可用；\n" +
            "movementType：报废出库551;零价格出库Z55;零价格入库Z56;收货多收Z16;串货调整309;不可抗力出库Z13;IT专用出库Z47;期初库存增加561;期初库存减少562;建安固定资产领用Z57;建安固定资产领用撤销Z58;\n" +
            "示例参数：\n" +
            "uuid = d4404090d679654de0530101007fb427, costCenter = GF3110143, postingDate = 2021-11-01, dataVersion = 7514\n" +
            "561, 562 提交SAP成本中心传空值；\n" +
            "", httpMethod = "POST")
    public ResponseResult submitOtherTaskSap(@RequestParam String uuid,
                                             @RequestParam String costCenter,
                                             @RequestParam String postingDate,
                                             @RequestParam Integer dataVersion){
        try {
            String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            return iOverseaOtherTaskService.submitOtherTaskSap(uuid, costCenter, postingDate, dataVersion, userId);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/cancelOtherTask")
    @ApiOperation(value = "其他出入库撤销", notes = "其他出入库撤销\n" +
            "spmsStatus：SPMS状态(0已删除;1保存;2提交/待审批;3审批不通过;4审批通过;5提交SAP失败;6提交SAP成功;)\n" +
            "movementType：报废出库551;零价格出库Z55;零价格入库Z56;收货多收Z16;串货调整309;不可抗力出库Z13;IT专用出库Z47;期初库存增加561;期初库存减少562;建安固定资产领用Z57;建安固定资产领用撤销Z58;\n" +
            "任务回撤；2-3(审批不通过)、6-4(撤回SAP凭证)、4/5-1(退到审批前，库存操作)，此功能仅对状态 2、4、5、6 可用；\n" +
            "示例参数：\n" +
            "uuid = d4404090d679654de0530101007fb427, \n" +
            "cancelPostingDate = 2021-11-01, 仅在状态6必输,\n" +
            "dataVersion = 7539\n" +
            "", httpMethod = "POST")
    public ResponseResult cancelOtherTask(@RequestParam String uuid, String cancelPostingDate,
                                             @RequestParam Integer dataVersion){
        try {
            String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            return iOverseaOtherTaskService.cancelOtherTask(uuid, cancelPostingDate, dataVersion, userId);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }
}
